﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using JBeamLibrary;

namespace DriveBeamEditor.Forms
{
    public partial class NewVehicleForm : Form
    {
        #region Properties

        /// <summary>Gets the created vehicle</summary>
        public Vehicle Vehicle
        {
            get;
            private set;
        }

        #endregion

        #region CTORs

        public NewVehicleForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        protected override void OnLoad(EventArgs e)
        {
            kvcVehicleCreator.Value = Config.Map["userName"];
            base.OnLoad(e);
        }

        private void btAddPart_Click(object sender, EventArgs e)
        {
            /* No part name entered */
            if (string.IsNullOrEmpty(kvcPartName.Value))
            {
                MessageBox.Show(
                    this,
                    @"Please enter a vehicle part name",
                    @"No name specified",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                return;
            }

            /* Entered part name has already been added */
            if (lbPartList.Items.Contains(kvcPartName.Value))
            {
                MessageBox.Show(
                    this,
                    string.Format(@"The '{0}' part has already been added", kvcPartName.Value),
                    @"Existing part name",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                return;
            }
            
            /* Add part to list */
            lbPartList.Items.Add(kvcPartName.Value);
        }

        private void btRemovePart_Click(object sender, EventArgs e)
        {
            if (lbPartList.SelectedIndex < 0) return;

            lbPartList.Items.RemoveAt(lbPartList.SelectedIndex);
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            /* No vehicle name specified */
            if (string.IsNullOrEmpty(kvcVehicleName.Value))
            {
                MessageBox.Show(
                    this,
                    @"Please enter a vehicle name",
                    @"No name specified",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                return;
            }

            Vehicle = CreateVehicle();

            DialogResult = DialogResult.OK;
            if (!Modal) Close();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            if (!Modal) Close();
        }

        #endregion

        #region Methods

        /// <summary>Creates a new vehicle based on the specified info</summary>
        /// <returns></returns>
        private Vehicle CreateVehicle()
        {
            var vehicle = new Vehicle(kvcVehicleName.Value);
            foreach (var part in lbPartList.Items)
            {
                vehicle.AddVehiclePart((string)part, (string)part);
            }
            return vehicle;
        }

        #endregion
    }
}
